package com.sf.guava.basic;

import com.google.common.base.*;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * @Classname StringsTest
 * @Description String 支持
 * @Date 2019/12/15 0015 18:36
 * @Created by 埔枘
 */
public class StringsTest {

    public static void main(String[] args) throws IOException {
        caseFormatTest();
    }


    public static void StringsTest(){
        // 判断 是否为null 或是 Empty
        System.out.println(Strings.isNullOrEmpty(null));
        // 如果是 null 或是 "" 转换成 null
        System.out.println(Strings.emptyToNull("123"));
        // null 转成 ""
        System.out.println(Strings.nullToEmpty(null));
        // 构建 重复 count 个数的值
        System.out.println(Strings.repeat("a",10));
        // 按长度填充,补充在前面
        System.out.println(Strings.padStart("123456",10,'a'));
        // 按长度填充,补充在后面
        System.out.println(Strings.padEnd("12345",10,'a'));
        // 返回匹配的前缀
        System.out.println(Strings.commonPrefix("ac2345","acvbfdgsdf"));
        // 返回匹配的后缀
        System.out.println(Strings.commonSuffix("asdasdasef","acvbfdgsdf"));
        // 模板填充
        System.out.println(Strings.lenientFormat("this is %s","ak47"));
    }

    /**
     * 拼接
     * @throws IOException
     */
    public static void joinerTest() throws IOException {
        List<Integer> eleList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, null);
        //分隔符
        //线程安全--不可变类
        String joinStr = Joiner.on("#")
                // 替换 null 内容 or skipNulls 二选一
                .useForNull("ak47")
                //过滤null元素
//                .skipNulls()
                //要分割的集合
                .join(eleList);
        //console : 1#2#3#4#5#6#7
        System.out.println(joinStr);

        FileWriter fileWriter = new FileWriter(new File("path.txt"));
        List<String> dateList = new ArrayList<>();
        dateList.add("123");
        dateList.add("abc");
        Joiner joiner = Joiner.on("#").useForNull(" ");
        //returns the FileWriter instance with the values appended into it
        FileWriter fileWriter1 = joiner.appendTo(fileWriter, dateList);
        fileWriter1.write("test context");
        fileWriter1.flush();

        Map<String, String> testMap = Maps.newLinkedHashMap();
        testMap.put("Washington D.C", "Redskins");
        testMap.put("New York City", "Giants");
        testMap.put("Philadelphia", "Eagles");
        testMap.put("Dallas", "Cowboys");
        String returnedString = Joiner.on("#").withKeyValueSeparator("=").join(testMap);
        // Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys
        System.out.println(returnedString);
    }

    /**
     *  分割
     */
    public static void splitterTest(){
        //JDK 实现的缺陷范例
//        String[] split = ",a,,b,".split(",");
//        for(int i=0;i<split.length;i++){
//            System.out.print(split[i]);
//        }

        String str="1,2,3,4,5,6,7    ";
        List<String> stringList = Splitter.on(",")
                // 去空格
//                .trimResults()
                .trimResults(new CharMatcher() {
                    @Override
                    public boolean matches(char c) {
                        //忽略 符合条件的
                        if(c == 49){
                            return true;
                        }
                        return false;
                    }
                })
                //过滤空字符串
                .omitEmptyStrings()
                //分路器，类似分页
//                .limit(2)
                .splitToList(str);
        System.out.println(stringList);

        String str2="name=zhang&age=18";
        Map<String, String> split = Splitter.on("&")
                .withKeyValueSeparator("=")
                .split(str2);
        System.out.println(split);
    }


    public static void caseFormatTest(){
        //constantName  大写下划线  转 小写驼峰
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME"));
        //abcDfg        小写连字符 转小写驼峰
        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "abc-dfg"));
    }
}
